resource "google_compute_network_endpoint" "<%= ctx[:primary_resource_id] %>" {
  network_endpoint_group = google_compute_network_endpoint_group.neg.name

  instance   = google_compute_instance.endpoint-instance.name
  port       = google_compute_network_endpoint_group.neg.default_port
  ip_address = google_compute_instance.endpoint-instance.network_interface[0].network_ip
}

data "google_compute_image" "my_image" {
  family  = "debian-9"
  project = "debian-cloud"
}

resource "google_compute_instance" "endpoint-instance" {
  name         = "<%= ctx[:vars]['instance_name'] %>"
  machine_type = "e2-medium"

  boot_disk {
    initialize_params {
      image = data.google_compute_image.my_image.self_link
    }
  }

  network_interface {
    subnetwork = google_compute_subnetwork.default.id
    access_config {
    }
  }
}

resource "google_compute_network_endpoint_group" "group" {
  name         = "<%= ctx[:vars]['neg_name'] %>"
  network      = google_compute_network.default.id
  subnetwork   = google_compute_subnetwork.default.id
  default_port = "90"
  zone         = "us-central1-a"
}

resource "google_compute_network" "default" {
  name                    = "<%= ctx[:vars]['network_name'] %>"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "default" {
  name          = "<%= ctx[:vars]['subnetwork_name'] %>"
  ip_cidr_range = "10.0.0.1/16"
  region        = "us-central1"
  network       = google_compute_network.default.id
}
